package com.yswee.mallnavi.view.brand;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.yswee.framework.utils.UnitUtils;
import com.yswee.mallnavi.R;


public class LetterHView extends LetterView {

	public LetterHView(Context context) {
		this(context, null);
	}

	public LetterHView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@SuppressWarnings("deprecation")
	protected void onDraw(Canvas canvas) {
		onSuperDraw(canvas);
		if (showBkg) {
			canvas.drawColor(getResources().getColor(R.color.widget_letter_bg));
		}

		int height = getHeight();
		int width = getWidth() - UnitUtils.dip2pix(getContext(), 6);
		int singleWidth = width / b.length;
		for (int i = 0; i < b.length; i++) {
			paint.setTextSize(fontSize);
			paint.setColor(fontColor);
			//paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setAntiAlias(true);
			if (i == choose) {
				paint.setColor(selColor);
				paint.setFakeBoldText(true);
			}
			float xPos = singleWidth * i + singleWidth / 2;
			float yPos = height - (height - this.fontSize) / 2;

			if (i == choose) {
				float size = this.fontSize;
				float centerX = xPos + paint.measureText(b[i]) / 2;
				float radius = paint.measureText(b[i]) / 2 + getContext().getResources().getDimension(R.dimen.dp_3);
				RectF rectF = new RectF(centerX - radius,
						height / 2 - radius,
						centerX + radius,
						height / 2 + radius);
				canvas.drawOval(rectF, paintBg);
			}

			canvas.drawText(b[i], xPos, yPos, paint);

			paint.reset();
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final float x = event.getX();
		final int oldChoose = choose;
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		final int c = (int) (x / getWidth() * b.length);

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			showBkg = true;
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c], y, x);
					choose = c;
					invalidate();
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c], y, x);
					choose = c;
					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			showBkg = false;
			choose = -1;
			invalidate();
			break;
		}
		return true;
	}

}